package com.iyao_app.im.presenter;

import com.iyao_app.im.uitl.ThreadUtil;
import com.iyao_app.im.view.ChatView;
import com.netease.nimlib.sdk.msg.constant.AttachStatusEnum;
import com.netease.nimlib.sdk.msg.model.IMMessage;

import java.util.ArrayList;
import java.util.List;

/**
 * Created by xue on 2017/11/21.
 */

public class ChatPresenterImpl implements ChatPresenter {

    private ChatView mChatView;
    private List<IMMessage> emMessageList = new ArrayList<>();

    public ChatPresenterImpl(ChatView mChatView) {
        this.mChatView = mChatView;
    }

    @Override
    public void sendMessage(final IMMessage emMessage) {
        //先添加到emMessageList中
        emMessage.getAttachStatus();//正在发送
        emMessageList.add(emMessage);
        //然后让Adapter Notify一下
        mChatView.updateChatData(true,null,emMessage);
        //发送的时候舰艇消息的发送状态 正在发送  成功  失败
//        emMessage.setMessageStatusCallback(new EMCallBackApdater(){
//            @Override
//            public void onSuccess() {
//                super.onSuccess();
//                afterSend(true,null,emMessage);
//            }
//
//            @Override
//            public void onError(int i, String s) {
//                super.onError(i, s);
//                afterSend(false,s,emMessage);
//            }
//        });
//        //通过环信sdk发送出去
//        EMClient.getInstance().chatManager().sendMessage(emMessage);

    }

    private void afterSend(final boolean success, final String msg, final IMMessage emMessage){
        //重新让Adapter Notify一下
        ThreadUtil.runOnUiThread(new Runnable() {
            @Override
            public void run() {
                mChatView.updateChatData( success, msg, emMessage);
            }
        });
    }


    @Override
    public void initChatData(String username, boolean isSmooth) {
      //  EMConversation conversation = EMClient.getInstance().chatManager().getConversation(username);

//        if (conversation == null){
//            emMessageList.clear();
//            mChatView.afterInitData(emMessageList,false);
//        }else {

//            //既然数据已经显示了,就认为用户已读了消息
//            conversation.markAllMessagesAsRead();
//
//            //统计数据库中我和username的所以的聊天记录的个数
//            int allMsgCount = conversation.getAllMsgCount();
//
//            //需要获取最新的一条消息
//            IMMessage lastMessage = conversation.getLastMessage();
//
//            allMsgCount = allMsgCount > 19 ? 19 : allMsgCount;
//
//            List<IMMessage> emMessages = conversation.loadMoreMsgFromDB(lastMessage.getMsgId(), 19);
//
//            emMessageList.clear();
//
//            emMessageList.addAll(emMessages);
//            emMessageList.add(lastMessage);
//
//            mChatView.afterInitData(emMessageList,isSmooth);
//        }
    }
}


